package org.example.service.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.dao.dao.user.UAddressDao;
import org.example.dao.dao.user.UStudentDao;
import org.example.dao.dao.user.UUserDao;
import org.example.dao.dto.user.UAddressDto;
import org.example.dao.dto.user.UserDto;
import org.example.dao.entity.post.PPost;
import org.example.dao.entity.user.UAddress;
import org.example.dao.entity.user.UStudent;
import org.example.dao.entity.user.UUser;
import org.example.skytool.commom.ContextUtil;
import org.example.skytool.commom.IdWorker;
import org.example.utils.user.RedisUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 22449
 */
@Service("addressService")
@Slf4j
public class AddressServiceImpl {


    @Resource
    private UUserDao uUserDao;


    @Resource
    private IdWorker idWorker;


    @Resource
    private UAddressDao uAddressDao;


    @Resource
    private ContextUtil contextUtil;

    @Resource
    private UStudentDao uStudentDao;


    public UAddress addAddress(String title, String schoolName, String dormitoryBuilding,
                               String houseNumber, String name, String mobile, String province,
                               String city, String region) {
        Long id = idWorker.nextId();
        Long userId = contextUtil.getId();
        UAddress uAddress = new UAddress(id, title, userId, schoolName, dormitoryBuilding, houseNumber
                , name, mobile, province, city, region);
        uAddressDao.insert(uAddress);
        return uAddress;
    }

    public void deleteAddress(Long id) {
        UAddress uAddress = uAddressDao.selectById(id);
        uAddress.setIsValid(0);
        uAddress.setUpdateTime(new Date());
        uAddressDao.updateById(uAddress);
    }

    @Resource
    private RedisUserService redisUserService;

    public IPage<UAddressDto> getAddress(Integer start,Integer limit) {
        Long id = contextUtil.getId();
        UserDto userDto = redisUserService.getUserDto(id.toString());
        Long defaultAddress = userDto.getUser().getDefaultAddress();

        LambdaQueryWrapper<UAddress> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UAddress::getIsValid, 1);
        lambdaQueryWrapper.eq(UAddress::getUserId, id);
        lambdaQueryWrapper.orderBy(true, false, UAddress::getUpdateTime);

        IPage<UAddress> pages = uAddressDao.selectPage(new Page<>(start,limit),lambdaQueryWrapper);


        IPage<UAddressDto> pageDto = new Page<>();
        List<UAddressDto> list = new ArrayList<>();

        for (UAddress address : pages.getRecords()) {
            UAddressDto addressDto = new UAddressDto();
            addressDto.setAddress(address);
            if (defaultAddress == null || !defaultAddress.equals(address.getId())) {
                addressDto.setDefaultAddress(false);
            } else {
                addressDto.setDefaultAddress(true);
            }
            list.add(addressDto);
        }
        pageDto.setTotal(pages.getTotal());
        pageDto.setSize(pages.getSize());
        pageDto.setCurrent(pages.getCurrent());
        pageDto.setPages(pages.getPages());
        pageDto.setRecords(list);
        return pageDto;
    }


    public UAddress updateAddress(String title, String schoolName, String dormitoryBuilding,
                                 String houseNumber, String name,
                                 String mobile, String province, String city,
                                 String region, Long id) {

        UAddress uAddress = uAddressDao.selectById(id);
        uAddress.setTitle(title);
        uAddress.setDormitoryBuilding(dormitoryBuilding);
        uAddress.setSchoolName(schoolName);
        uAddress.setHouseNumber(houseNumber);
        uAddress.setName(name);
        uAddress.setMobile(mobile);
        uAddress.setProvince(province);
        uAddress.setCity(city);
        uAddress.setRegion(region);
        uAddress.setUpdateTime(new Date());
        uAddressDao.updateById(uAddress);
        return uAddress;
    }

}
